package com.hymjweb.demo.hymj.system_maintenance.role.controller;

import com.hymjweb.demo.framework.result.BaseResultMap;
import com.hymjweb.demo.framework.sys.BaseController;
import com.hymjweb.demo.framework.sys.SysConstant;
import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.system_maintenance.role.bean.RolePojo;
import com.hymjweb.demo.hymj.system_maintenance.role.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
@RequestMapping("/pages/system_maintain/role")
public class RoleController extends BaseController {

    @Autowired
    private RoleService roleService;


    @RequestMapping("/roleInit")
    public String roleInit() {
        return "pages/system_maintain/role/roleInit";
    }

    @RequestMapping("/queryRoleList")
    @ResponseBody
    public Object queryRoleList(@RequestParam(required = false) Map param) throws Exception {
        return roleService.getPageList(param);
    }


    @RequestMapping("/checkNameUnique")
    @ResponseBody
    public Object checkNameUnique(String name) throws Exception {

        if (DataTypeUtil.isEmptyStr(name)) {
            throwException("角色名称为空参数错误！");
        }

        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("角色名称检测通过!");

        RolePojo rolePojo = roleService.checkNameUnique(name);

        if (rolePojo != null) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("角色名称【" + name + "】存在重复!");
        }

        return baseResultMap;
    }

    @RequestMapping("/addRole")
    @ResponseBody
    public Object addRole(@RequestBody RolePojo rolePojo) throws Exception {

        if (rolePojo == null) {
            throwException("角色添加参数错误！");
        }

        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("角色添加成功!");

        int affectRows = roleService.addRole(rolePojo);

        if (affectRows <= SysConstant.INT_ZERO) {
            baseResultMap.setOk(true);
            baseResultMap.setMessage("角色添加失败!");
        }

        return baseResultMap;
    }


    @RequestMapping("/updateRole")
    @ResponseBody
    public Object updateRole(@RequestBody RolePojo rolePojo) throws Exception {

        if (rolePojo == null) {
            throwException("角色更新参数错误！");
        }

        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("角色更新成功!");

        int affectRows = roleService.updateRole(rolePojo);

        if (affectRows <= SysConstant.INT_ZERO) {
            baseResultMap.setOk(true);
            baseResultMap.setMessage("角色更新失败!");
        }

        return baseResultMap;
    }

}
